﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Glucometer.ModuleHandler;
using Glucometer.Utility;

namespace Glucometer
{
    public enum SoftwareEventsType
    {
        SHORT_CLICK,
        LONG_CLICK,
        DOUBLE_CLICK,
        UNDEFINED_CLICK,
        USB_INSERTED,
        USB_REMOVED,
        STRIP_INSERTED,
        STRIP_REMOVED,
        AC_PLUGGED_IN,
        AC_PLUGGED_OUT,
        VALID_STRIP,
        INVALID_STRIP,
        SUFFICIENT_BLOOD,
        INSUFFICIENT_BLOOD,
        RESULT_READY,
        BATTERY_LEVEL_DOWN,
        BATTERY_LEVEL_UP,
        TIME_TICK,
        NONE
    }

    public delegate void d_SoftwareEvents(SoftwareEventsType set);

    class HardWareInterruptInterpreter
    {
        IHarware _invoker;

        public Battery _battery;

        ButtonClickInterpreter _buttonClickInterpreter;

        public event d_SoftwareEvents SoftwareEventOccurred;

        public HardWareInterruptInterpreter(IHarware interrupt, ButtonClickInterpreter bci, Battery battery)
        {

            this._invoker = interrupt;

            this._buttonClickInterpreter = bci;

            this._battery = battery;
            
            interrupt.RaiseInterrupt += new d_Interrupt(InterruptHandler);

            _buttonClickInterpreter.ButtonClick += new d_ButtonClick(buttonClickInterpreter_ButtonClick);

            this._battery.BatteryLevelDecreased += new d_EmptyArgument(_battery_BatteryLevelDecreased);

            this._battery.BatteryLevelIncreased += new d_EmptyArgument(_battery_BatteryLevelIncreased);



            System.Timers.Timer t = new System.Timers.Timer();
            t.AutoReset = true;
            t.Interval = 1000;
            t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
            t.Start();
        }

        public int BatteryLevel { get { return _battery.BatteryLevel; } }

        void _battery_BatteryLevelIncreased()
        {
            if (SoftwareEventOccurred != null)
                SoftwareEventOccurred(SoftwareEventsType.BATTERY_LEVEL_UP);
        }

        void _battery_BatteryLevelDecreased()
        {
            if (SoftwareEventOccurred != null)
                SoftwareEventOccurred(SoftwareEventsType.BATTERY_LEVEL_DOWN);
        }

        public void RaiseBatteryInterrupt(HardwareInterrupt interrupt)
        {
            if (interrupt == HardwareInterrupt.BATTERY_LEVEL_DOWN && SoftwareEventOccurred != null)
                SoftwareEventOccurred(SoftwareEventsType.BATTERY_LEVEL_DOWN);


            if (interrupt == HardwareInterrupt.BATTERY_LEVEL_UP && SoftwareEventOccurred != null)
                SoftwareEventOccurred(SoftwareEventsType.BATTERY_LEVEL_UP);
        }

        void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (SoftwareEventOccurred != null)
                SoftwareEventOccurred(SoftwareEventsType.TIME_TICK);
        }

        void buttonClickInterpreter_ButtonClick(ButtonClickType clickType)
        {
            if (SoftwareEventOccurred != null)
            {
                switch (clickType)
                {
                    case ButtonClickType.SHORT_CLICK:
                        SoftwareEventOccurred(SoftwareEventsType.SHORT_CLICK);
                        break;
                    case ButtonClickType.LONG_CLICK:
                        SoftwareEventOccurred(SoftwareEventsType.LONG_CLICK);
                        break;
                    case ButtonClickType.DOUBLE_CLICK:
                        SoftwareEventOccurred(SoftwareEventsType.DOUBLE_CLICK);
                        break;
                    case ButtonClickType.UNDEFINED:
                        SoftwareEventOccurred(SoftwareEventsType.UNDEFINED_CLICK);
                        break;
                    default:
                        break;
                }
            }
        }

        void InterruptHandler(HardwareInterrupt interrupt)
        {
            if (SoftwareEventOccurred != null)
            {
                if (interrupt == HardwareInterrupt.BUTTON_DOWN || interrupt == HardwareInterrupt.BUTTON_UP)
                {
                    this._buttonClickInterpreter.HandleHardwareInterrupt(interrupt, DateTime.Now);
                }
                else if (interrupt == HardwareInterrupt.USB_INSERTED)
                    SoftwareEventOccurred(SoftwareEventsType.USB_INSERTED);
                else if (interrupt == HardwareInterrupt.USB_REMOVED)
                    SoftwareEventOccurred(SoftwareEventsType.USB_REMOVED);
                else if (interrupt == HardwareInterrupt.AC_PLUGGED_IN)
                {
                    SoftwareEventOccurred(SoftwareEventsType.AC_PLUGGED_IN);
                }
                else if (interrupt == HardwareInterrupt.AC_PLUGGED_OUT)
                    SoftwareEventOccurred(SoftwareEventsType.AC_PLUGGED_OUT);
                else if (interrupt == HardwareInterrupt.STRIP_INSERTED)
                {
                    SoftwareEventOccurred(SoftwareEventsType.STRIP_INSERTED);
                }
                else if (interrupt == HardwareInterrupt.STRIP_REMOVED)
                {
                    SoftwareEventOccurred(SoftwareEventsType.STRIP_REMOVED);
                }
                else if (interrupt == HardwareInterrupt.VALID_STRIP)
                {
                    SoftwareEventOccurred(SoftwareEventsType.VALID_STRIP);
                }
                else if (interrupt == HardwareInterrupt.INVALID_STRIP)
                {
                    SoftwareEventOccurred(SoftwareEventsType.INVALID_STRIP);
                }
                else if (interrupt == HardwareInterrupt.SUFFICIENT_BLOOD)
                {
                    SoftwareEventOccurred(SoftwareEventsType.SUFFICIENT_BLOOD);
                }
                else if (interrupt == HardwareInterrupt.INSUFFICIENT_BLOOD)
                {
                    SoftwareEventOccurred(SoftwareEventsType.INSUFFICIENT_BLOOD);
                }
                else if (interrupt == HardwareInterrupt.RESULT_READY)
                {
                    SoftwareEventOccurred(SoftwareEventsType.RESULT_READY);
                }
            }
        }
    }
}